package com.tx.service.impl;

import com.tx.dao.FileInfoDao;
import com.tx.entity.FileInfo;
import com.tx.entity.Page;
import com.tx.entity.Result;
import com.tx.service.FileInfoService;
import com.tx.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: tcx
 * @description: 文件管理业务层实现类
 * @Date: 2019/11/10
 **/
@Service
public class FileInfoServiceImpl implements FileInfoService {
    //注入数据持久层接口
    @Autowired
    private FileInfoDao fileInfoDao;
    public Integer add(FileInfo fileInfo) {
        //文件上传时间
        fileInfo.setfCreateTime(new Date());
        return fileInfoDao.insert(fileInfo);
    }

    public Integer edit(FileInfo fileInfo) {
        return fileInfoDao.update(fileInfo);
    }

    public Integer remove(Integer fId) {
        return fileInfoDao.delete(fId);
    }

    public FileInfo get(Integer fId) {
        return fileInfoDao.select(fId);
    }

    public List<FileInfo> getAll() {
        return fileInfoDao.selectAll();
    }

    /* *
     * @description:  根据用户分页查询文件
     * @author tcx
     * @date 2019/11/14
     * @param uId
     * @return com.tx.entity.Result
     */
    public Result getFileByUser(Integer uId,Page page) {
        //获得用户博客总记录
        Integer count = fileInfoDao.selectFileByUserCount(uId);
        page = PageUtils.createPage(page,count);
        List<FileInfo> list = fileInfoDao.selectFileByUser(uId,page.getBeginIndex(),page.getEveryPage());
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }

    public Integer getFileByUserCount(Integer uId) {
        return fileInfoDao.selectFileByUserCount(uId);
    }
    /* *
     * @description:  根据关键字分页查询文件
     * @author tcx
     * @date 2019/11/13
     * @param key
     * @param page
     * @return java.util.List<com.tx.entity.FileInfo>
     */
    public Result getKeyPage(String key, Page page) {
        //获得分类查询博客总记录数
        Integer count = fileInfoDao.selectFileKeyCount(key);
        page = PageUtils.createPage(page,count);
        List<FileInfo> list = fileInfoDao.selectKeyPage(key,page.getBeginIndex(),page.getEveryPage());
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }
    /* *
     * @description:  分页查询所有文件
     * @author tcx
     * @date 2019/11/13
     * @param page
     * @return java.util.List<com.tx.entity.FileInfo>
     */
    public Result getFileByPage(Page page) {
        //获得博客总记录数
        Integer count = fileInfoDao.selectFileAllCount();
        page = PageUtils.createPage(page,count);
        List<FileInfo> list = fileInfoDao.selectFileByPage(page);
        Result result = new Result();
        result.setPage(page);
        result.setList(list);
        return result;
    }
    /* *
     * @description:  查询用户最新十个文件
     * @author tcx
     * @date 2019/11/16
     * @param uId
     * @return java.util.List<com.tx.entity.FileInfo>
     */
    public List<FileInfo> getUserNewFile(Integer uId) {
        return fileInfoDao.selectUserNewFile(uId);
    }
    /* *
     * @description:  查询所有文件总数
     * @author tcx
     * @date 2020/1/13
     * @param
     * @return int
     */
    public int getFileCount() {
        return fileInfoDao.selectFileAllCount();
    }
    public Integer getTotal(Map<String, Object> map) {
        return fileInfoDao.getTotal(map);
    }

    public List<FileInfo> list(Map<String, Object> map) {
        return fileInfoDao.selectFileKey(map);
    }
}
